package com.usian.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Auther：DONG on 2021/11/4
 **/
@RestController
@RequestMapping("/api/v1/author")
public class ApAuthorController implements AuthorControllerApi {
    @Autowired
    private ApAuthorService apAuthorService;

    @Override
    @GetMapping("/findByUserId/{id}")
    public ApAuthor findByUserId(@PathVariable Integer id) {
        LambdaQueryWrapper<ApAuthor> lambdaQueryWrapper = Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, id);
        List<ApAuthor> authorlist = apAuthorService.list(lambdaQueryWrapper);
            if (authorlist!=null && !authorlist.isEmpty()){
                return authorlist.get(0);
            }

        return null;
    }

    @Override
    @PostMapping("/save")
    public boolean save(@RequestBody ApAuthor apAuthor) {
        return apAuthorService.save(apAuthor);
    }
}
